<?php


namespace app\api\controller;


use app\BaseController;
use think\App;
use app\logic\traits\Token;
use Request\Key;
use app\api\common as wxappCommon;
use app\api\ControllerBase as wxappController;
class Common extends BaseController
{
    protected $auth;
    protected $needLogin;//需要登录的方法
    protected $noLogin=[
    	'selectNews',
    	'findNewsDetails',
    	'selectNewsComment',
    	'selectRecomment',
    ];
    use Token;
    public $_no_login=false;
    public function __construct(App $app)
    {
        global $user;
        parent::__construct($app);
        $needLogin=$this->needLogin;
        $_cotrollecr_obj=new wxappController();
        if(!$_cotrollecr_obj->checkParam($app->request)){
        	return wxappCommon::return_json_layui($data, $_key_obj->error, $_key_obj->code);
        }
        if(!$this->request->header('token') && in_array($this->request->action(),$this->noLogin)){
        	$this->_no_login=1;
        	return true;
        }
        if($needLogin && in_array($this->request->action(),$needLogin)){
            $token = $this->request->header('token');
            if($token){
                list($code,$msg)=$this->GetToken($token); //获取token
                if($code){  //校验成功啦！！
                    /*if($msg['status'] != 1){
                        $this->error('你已经被封禁了',[],999999);
                    }*/
                    $user = $msg;
                }else{
                    $this->needLogin($msg);
                }
            }else{
                $this->needLogin();
            }
        }else{  //该地方首页需要去验证是否登录，弱验证方式
            $token = $this->request->header('token');
            if($token){
                list($code,$msg) = $this->GetToken($token);
                if($code){
                    $user = $msg;
                }
            }
        }
    }

    /**
     * @param $msg
     * @return void
     */
    protected function needLogin($msg="token为空"){
        show_json("wxapp.login.need_login",$msg,true);
    }
}